import {
    createSelector
} from "reselect"


import { reqGetcate, reqGetgoods, reqGetgroupon, reqGetindexgoods, reqGetnew, reqGetseckill } from "../../http/api"


// 数据 初始值
const initState = {
    // 1.1新人专享的初始值
    getnewList: [],
    // 2.1万人团的初始值
    getGroupon: [],
    // 3.1秒杀
    getseckill:[],
    // 4.1分类
    cate:[],
    // 5.1 分类下的商品初始值
    goods:[],
    // 5.1.1
    n:0
}


//action types
const types = {
    // 1.2修改新人专享的type
    HOME_CHANGE_BANNER:"HOME_CHANGE_BANNER",
    // 2.2修改万人团的type
    HOME_CHANGE_GROUPON:"HOME_CHANGE_GROUPON",
    // 3.2
    HOME_CHANGE_SECKILL:"HOME_CHANGE_SECKILL",
    // 4.2分类的type
    HOME_CHANGE_CATE:"HOME_CHANGE_CATE",
    // 5.2分类下的商品的type
    HOME_CHANGE_GOODS:"HOME_CHANGE_GOODS",
    // 5.1.2
    HOME_CHANGE_N:"HOME_CHANGE_N"
}



// actions creater
export const actions = {
    // 1.4修改banner的action
    changeGetnewList:getnewList=>({type:types.HOME_CHANGE_BANNER,getnewList}),
    // 1.6逻辑 请求轮播图
    reqGetnewListAction:()=>{
        return (dispatch,getState)=>{
            // 发请求
            reqGetnew().then(res=>{
                if(res.data.code===200){
                    // 修改banner的准备
                    dispatch(actions.changeGetnewList(res.data.list))
                }
            })
        }
    },

    // 2.4修改万人团轮播图的action
    changeGetGroupon:getGroupon=>({type:types.HOME_CHANGE_GROUPON,getGroupon}),
    // 2.6逻辑
    reqGetGrouponAction:()=>{
        return (dispatch,getState)=>{
            reqGetgroupon().then(res=>{
                if(res.data.code===200){
                    // 修改banner的准备
                    dispatch(actions.changeGetGroupon(res.data.list))
                }
            })
        }
    },


    // 3.4
    changeGetseckill:getseckill=>({type:types.HOME_CHANGE_SECKILL,getseckill}),
    // 3.6
    reqGetseckillAction:()=>{
        return (dispatch,getState)=>{
            reqGetseckill().then(res=>{
                if(res.data.code===200){
                    // 修改banner的准备
                    dispatch(actions.changeGetseckill(res.data.list))
                }
            })
        }
    },
    


    // 4.4修改擦特的action
    changeCate:cate=>({type:types.HOME_CHANGE_CATE,cate}),
    // 4.6请求cate
    reqCateAction:()=>(dispatch,getState)=>{
        reqGetcate().then(res=>{
            if(res.data.code===200){
                // 修改cate
                dispatch(actions.changeCate(res.data.list))
            }
        })
    },



    // 5.4修改分类下的商品的action
    changeGoods:goods=>({type:types.HOME_CHANGE_GOODS,goods}),
    // 5.6
    reqGoodsAction:()=>(dispatch,getState)=>{
        reqGetindexgoods().then(res=>{
            if(res.data.code===200){
                // 修改cate
                dispatch(actions.changeGoods(res.data.list))
            }
        })
    },
    

    // 5.1.3
    changeN:n=>({type:types.HOME_CHANGE_N,n})
}




// reducer
export const homeReducer = (state = initState, action) => {
    switch(action.type){
        // 1.3修改新人专享轮播图的逻辑
        case types.HOME_CHANGE_BANNER:
            return {
                ...state,
                getnewList:action.getnewList
            }
            // 2.3修改万人团轮播图的逻辑
        case types.HOME_CHANGE_GROUPON:
            return {
                ...state,
                getGroupon:action.getGroupon
            }
            // 3.3修改秒杀轮播图的逻辑
        case types.HOME_CHANGE_SECKILL:
            return {
                ...state,
                getseckill:action.getseckill
            }
            // 4.3
        case types.HOME_CHANGE_CATE:
            return {
                ...state,
                cate:action.cate
            }
        // 5.3修改的逻辑
        case types.HOME_CHANGE_GOODS:
            return {
                ...state,
                goods:action.goods
            }
        // 5.1.4
        case types.HOME_CHANGE_N:
            return {
                ...state,
                n:action.n
            }

            default:
            return state;
    }
}

// 导出数据 一堆函数

// 1.5导出
export const getgetnewList = state=>state.home.getnewList
// 2.5导出
export const getgetGroupon=state=>state.home.getGroupon
// 3.5
export const getgetseckill=state=>state.home.getseckill
// 4.5导出
export const getcate=state=>state.home.cate
// 5.5导出
export const getgoods=state=>state.home.goods
// 5.1.5
export const getN=state=>state.home.n
// 5.1.7
export const getShowgoods=createSelector([getgoods, getN], (list, n)=>{
   return list.length>0? list[n].content:[]
})

